package com.example.fivedemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


import com.example.fivedemo.R;
import com.example.fivedemo.entity.Hot;

import java.util.ArrayList;
import java.util.List;

public class HotAdapter extends ArrayAdapter<Hot> {
    private List<Hot> hotList ;
    private LayoutInflater inflater ;
    private int resourceLayout ;
    public HotAdapter(@NonNull Context context, int resource,  @NonNull List<Hot> objects) {
        super(context, resource, objects);
        this.hotList = new ArrayList<>(objects) ;
        this.resourceLayout = resource ;
        inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null){
            convertView = inflater.inflate(resourceLayout , parent , false);
            viewHolder.id = convertView.findViewById(R.id.h_id) ;
            viewHolder.text = convertView.findViewById(R.id.h_text);
            viewHolder.num = convertView.findViewById(R.id.h_num);
            convertView.setTag(viewHolder);

            ViewHolder finalViewHolder = viewHolder;
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String click_id = finalViewHolder.id.getText().toString();
                    for (Hot hot : hotList){
                        if (click_id.equals(hot.getIndex())){
                            openWebsite(hot.getLink());
                        }
                    }
                }
            });
        }else {
            viewHolder = (HotAdapter.ViewHolder) convertView.getTag();
        }

        Hot hot = hotList.get(position);
        if (hot != null){
            viewHolder.id.setText(hot.getIndex());
            viewHolder.text.setText(hot.getTitle());
            viewHolder.num.setText(hot.getHotValue());
        }

        return convertView;
    }

    @Nullable
    @Override
    public Hot getItem(int position) {
        return hotList.get(position);
    }

    @Override
    public int getCount() {
        return hotList.size();
    }

    private static class ViewHolder{
        TextView id ;
        TextView text ;
        TextView num ;
    }

    //跳转浏览器
    private void openWebsite(String uri) {
        String url = uri; // 替换成你要跳转的网址
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        if (intent.resolveActivity(getContext().getPackageManager()) != null) {
            getContext().startActivity(intent);
        } else {
            // 没有可以处理此Intent的应用程序
            Toast.makeText(getContext(), "无法打开网页，请检查设备是否安装了浏览器", Toast.LENGTH_SHORT).show();
        }
    }

}
